package com.cn.mobileproject.view.adapter;

import java.util.ArrayList;

import javax.crypto.NullCipher;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;

import com.cn.mobileproject.R;
import com.cn.mobileproject.bean.Task;
import com.cn.mobileproject.data.GlobalValue;
import com.cn.mobileproject.utils.TextHelper;

public class MyTaskAdapter  extends BaseAdapter implements  OnClickListener{

	protected Context mContext;
	
	protected ArrayList<Task> mList;
	protected boolean  mCheck;
	protected TaskCheckClickListener mTaskCheckClickListener;
	
	public interface TaskCheckClickListener{
		public void onClick(Task task);
	}
	
	public void setTaskCheckClickListener(
			TaskCheckClickListener mTaskCheckClickListener) {
		this.mTaskCheckClickListener = mTaskCheckClickListener;
	}

	public MyTaskAdapter(Context context,boolean check){
		mContext = context;
		mCheck= check;
	}
	
	@Override
	public int getCount() {
		if(null==mList||mList.size()==0) return 0;
		return mList.size();
	}

	@Override
	public Object getItem(int position) {
		if(null==mList||mList.size()==0||position>=mList.size()) return null;
		return mList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}
	
	public void clear(){
		if(null!=mList) mList.clear();
	}
	
	class ViewHolder{
		TextView nameText;
		TextView codeText;
		TextView checkDateText;
		TextView checkPersonNameText;
		TextView handleDateText;
		CheckBox mCheckBox;
		Button iconBtn;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if(convertView == null){
			convertView = LayoutInflater.from(mContext).inflate(R.layout.instruct_item, null);
			holder = new ViewHolder();
			holder.nameText =(TextView) convertView.findViewById(R.id.instruct_item_text_ordername);
			holder.codeText =(TextView) convertView.findViewById(R.id.instruct_item_text_code);
			holder.checkDateText =(TextView) convertView.findViewById(R.id.instruct_item_text_time);
			holder.checkPersonNameText =(TextView) convertView.findViewById(R.id.instruct_item_text_checkPersons);
			holder.handleDateText = (TextView)convertView.findViewById(R.id.instruct_item_text_handtime);
			holder.mCheckBox =(CheckBox) convertView.findViewById(R.id.iv_right);
			holder.iconBtn = (Button)convertView.findViewById(R.id.instruct_item_text_icon);
			if(mCheck) holder.mCheckBox.setOnClickListener(this);
			convertView.setTag(holder);
		}
		else{
			holder = (ViewHolder) convertView.getTag();
		}
		
		if(null==mList||position>mList.size()){
			return convertView;
		}
		Task task = mList.get(position);
		holder.checkPersonNameText.setText(TextHelper.getFliteStr(task.checkPersonsName));
		holder.nameText.setText(TextHelper.getFliteStr(task.orderName));
		holder.codeText.setText(TextHelper.getFliteStr(task.orderNo));
		holder.checkDateText.setText(TextHelper.getFliteStr(task.checkDate));
		holder.handleDateText .setText(TextHelper.getFliteStr(task.handleDate));
//		holder.checkPersonNameText.setText(TextHelper.getFliteStr(instruct.checkPersonsName));
		if(task.isNew!=null&&!"".equals(task.isNew)){
			holder.iconBtn.setVisibility(View.VISIBLE);
		}
		else{
			holder.iconBtn.setVisibility(View.GONE);
		}
		
		if(mCheck){
			holder.mCheckBox.setVisibility(View.VISIBLE);
			holder.mCheckBox.setTag(task);
			String taskid = GlobalValue.getInstance().taskid;
			
			if(taskid !=null&&taskid.equals(task.taskid)){
				holder.mCheckBox.setChecked(true);
			}
			else{
				holder.mCheckBox.setChecked(false);
			}
		}
		else{
			holder.mCheckBox.setVisibility(View.GONE);
			
		}
		
		return convertView;
	}

	public ArrayList<Task> getList() {
		return mList;
	}

	public void setList(ArrayList<Task> list) {
		if(mList==null){
			this.mList = new ArrayList<Task>();
		}
		else{
			this.mList.clear();
		}
		if(list!=null){
			this.mList.addAll(list);
		}
	}

	@Override
	public void onClick(View v) {
		if(v instanceof CheckBox){
			Task task = (Task)v.getTag();
			if(mTaskCheckClickListener!=null){
				mTaskCheckClickListener.onClick(task);
			}
		}
	}
}
